﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using DailyDiet.Services;
using DailyDiet.Services.Constant;
using DailyDiet.Services.Navigation;
using DailyDiet.util.eventArgs;
using DailyDiet.Views;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using Xamarin.Forms;

namespace DailyDiet.ViewModels
{
    public class InitializeViewModel : ViewModelBase {
        private IPreferenceStorage _preference;
        private IDietStorageService _dietStorage;
        private IDietRecorderStorageService _dietRecorderStorage;
        private IFavoriteStorageService _favoriteStorage;
        private INotificationService _notificationService;

        private RelayCommand _appearingCommand;

        public RelayCommand AppearingCommand =>
            _appearingCommand ?? (_appearingCommand =
                new RelayCommand(async () => await AppearingCommandFunc()));

        public async Task AppearingCommandFunc() {
            if (!_dietRecorderStorage.IsInitialized())
            {
                await _dietRecorderStorage.InitializeAsync();
            }

            if (!_dietStorage.IsInitialized())
            {
                await _dietStorage.InitializeAsync();
            }

            if (!_favoriteStorage.IsInitialized())
            {
                await _favoriteStorage.InitializeAsync();
            }

            if (!_splitSentenceService.JiebaIsInitialize())
            {
                await _splitSentenceService.InitializeJieba();
            }

            string key = "isFillRecorderMessage";

            if (_preference.Get(key, -1) == -1)
            {
                await _recorderStorageFillMeassage.initial();
                _preference.Set(key, 1);
            }

            string[] keys = new[] {
                TasteSetConstant.HealthIndexKey, TasteSetConstant.SpicyIndexKey,
                TasteSetConstant.SweetIndexKey, TasteSetConstant.TasteIndexKey
            };

            foreach (var eachKey in keys)
            {
                _preference.Remove(eachKey);
                _preference.Get(eachKey, TasteSetConstant.DefaultIndex);
                if (_preference.Get(eachKey,
                        TasteSetConstant.DefaultIndex) ==
                    TasteSetConstant.DefaultIndex)
                {
                    _preference.Set(eachKey, 50);
                }
            }

            //TODO 设置通知
            var mealTimes =
                _notificationService.GetMealTimeByDatetime(DateTime.Now);

            await _notificationService.SetNotification(mealTimes);
        }

        private RelayCommand _go;

        public RelayCommand Go =>
            _go ?? (_go =
                new RelayCommand(async () => await Shell.Current.GoToAsync($"//HealthInfoPage")));

        //监听DietRecorder事件，如果被提前设置就不再通知了
        private void DietRecorderOnSet(object sender,
            DietRecorderStorageEventArgs e) {
            String mealTimes = e.MealTimes;
            _notificationService.DeleteNotification(mealTimes);
        }

        private IRecorderStorageFillMeassage _recorderStorageFillMeassage;
        private ISplitSentenceService _splitSentenceService;


        public InitializeViewModel(
            IPreferenceStorage preferenceStorage,
            IDietStorageService dietStorageService,
            IFavoriteStorageService favoriteStorageService,
            IDietRecorderStorageService dietRecorderStorage,
            IRecorderStorageFillMeassage recorderStorageFillMeassage,
            INotificationService notificationService,
            ISplitSentenceService splitSentenceService)
        {
            _preference = preferenceStorage;
            _dietRecorderStorage = dietRecorderStorage;
            _dietStorage = dietStorageService;
            _preference = preferenceStorage;
            _favoriteStorage = favoriteStorageService;
            _recorderStorageFillMeassage = recorderStorageFillMeassage;
            _dietRecorderStorage.DietRecorderStorageEvent += DietRecorderOnSet;
            _notificationService = notificationService;
            _splitSentenceService = splitSentenceService;
        }
    }
}
